{include file="public/meta"}
<title>添加广告</title>
<link rel="stylesheet" href="__PUBLIC__/css/ly-upload.css">
</head>
<body>
<article class="page-container">
    <form class="form form-horizontal" id="form-data-edit" enctype="multipart/form-data" action="{:url('banner/save')}" method="post">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>广告图片：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <label class="upload_lable" for="file_img"><img id="show_img" src="{$adsList.adFile}"/></label>
                <input type="file" name="img" id="file_img" value="{$adsList.adFile}" hidden="hidden"/>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>广告标题：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" id="title" name="title" value="{$adsList.adName}" autocomplete="off">
                 <input type="text" name="adId" id="adId" value="{$adsList.adId}" hidden="hidden"/>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;返回列表&nbsp;&nbsp;</button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <button id="bt_sub" class="btn btn-primary radius" type="button">&nbsp;&nbsp;创建&nbsp;&nbsp;</button>
            </div>
        </div>
    </form>
</article>

{include file="public/footer"}

<!--请在下方写此页面业务相关的脚本-->
<!-- <script type="text/javascript" src="__PUB__/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="__PUB__/datatables/1.10.0/jquery.dataTables.min.js"></script> -->
<script type="text/javascript">
    function toBase64(file, fun) {
        //参数file为input获取到的file数组第一项，fun向外传递转换成功的base64编码
        var reader = new FileReader();
        if (file) {
            reader.readAsDataURL(file);
            reader.onload = function () {
                fun(this.result);
            }
        }
    }

    $(function(){
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });

        $('#file_img').change(function () {
            var file_img=$(this);
            var img = $('#show_img');
            if (file_img[0].files[0].type.indexOf("image") == -1) {
                file_img.val("");
                img.attr('src',"{{ asset('static/ic_ph.png') }}");
                layer.msg('请选择图片类型的文件!', {icon: 7, time: 1000});
            } else {
                var files = file_img[0].files[0];
                toBase64(files, function (res) {
                    img.attr('src',res);
                })
            }
        });


        $('#bt_sub').click(function () {
            var bt_sub = $(this);
            var title = $('#title').val();
            if (title == '' || title == undefined) {
                layer.msg('请填写广告标题!', {icon: 7, time: 1000});
                return false;
            }
           
            bt_sub.attr('disabled', 'disabled');
            var formData = new FormData();
            formData.append('img',$("#file_img")[0].files[0]);
            $.each($("#form-data-edit").serializeArray(), function (i, field) {
                formData.append(field.name, field.value);
            });
            $.ajax({
                url: $('#form-data-edit').attr('action'),
                type: 'POST',
                data: formData,
                processData: false,// 告诉jQuery不要去处理发送的数据
                contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                beforeSend: function () {
                    layer.msg('正在上传，请不要刷新或关闭页面!', {icon: 7, time: 1000000});
                },
                success: function (res) {
                    if (res.state == 1) {
                        window.parent.layer.msg(res.msg, {icon: 1, time: 1000});
                        setTimeout(function () {
                            window.parent.location.reload();
                        }, 1200);
                    }else {
                        window.parent.layer.msg(res.msg, {icon: 1, time: 1000});
                        setTimeout(function () {
                            window.parent.location.reload();
                        }, 1200);
                    }
                },
                error: function (res) {
                    layer.msg('网络错误，请关闭重试!', {icon: 2, time: 1000});
                    bt_sub.removeAttr('disabled');
                }
            });
        });
    });
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>